//
//  ViewController.m
//  AVPlayerDemo
//
//  Created by YIDAOMAC on 2024/1/16.
//

#import "ViewController.h"
#import "VideoGridCell.h"
#import "GKVideoPlayerManager.h"
#import <AFNetworking/AFNetworking.h>
#import "VideoInfo.h"
#import <MJRefresh.h>
#import <KTVHTTPCache/KTVHTTPCache.h>

@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

///
@property(nonatomic, strong) UICollectionView *collectionView;

///
@property(nonatomic, strong) NSMutableArray<VideoInfo*> *infos;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSError *error = nil;
    [KTVHTTPCache proxyStart:&error];
    if (error) {
        NSLog(@"启动代理失败 %@", error);
    }
    self.infos = [NSMutableArray array];
    [self loadInfo];
}

- (void)loadInfo
{
    NSString *url = @"https://api-hl.huoshan.com/hotsoon/feed/?type=video&iid=56693189339&ac=WIFI&ab_version=391711,501253,592609,662547,671134,384501,663932,612165,681210,674736,654193,557631,678843,681232,680055,637814,666872,681693,661943,681616,374104,378844,682009,665355,446763,638535,681229,299910,632485,671292,651646,598627,641184,457535,493546,677944&os_api=18&app_name=live_stream&channel=App%20Store&idfa=59E6123C-45AE-4F64-9192-FD9B0E982923&device_platform=iphone&live_sdk_version=5.4.0&vid=DD5987BE-4451-43E1-B234-68525808A52F&mccmnc=&device_type=iPhone7,1&openudid=62948e3983ef2572b9acc1b524998172d40f5f73&version_code=5.4.0&os_version=12.0&screen_width=1125&aid=1112&device_id=36177005663&req_from=feed_refresh&action=refresh&diff_stream=1&mas=00f6d48a97d7aaa909e3451f22676861b28a07d5ae901270dbe287&as=a2b5c403499e6cf3806834&ts=1546666985";
    
    [AFHTTPSessionManager.manager GET:url parameters:nil headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
        NSArray *data = responseObject[@"data"];
        NSInteger begin = self.infos.count;
        for (NSDictionary *dic in data) {
            [self.infos addObject:[VideoInfo infoWithDictionary:dic[@"data"]]];
        }
        
        if (self.collectionView) {
            NSMutableArray *indexPaths = [NSMutableArray array];
            for (NSInteger i = begin; i < self.infos.count; i ++) {
                [indexPaths addObject:[NSIndexPath indexPathForItem:begin inSection:0]];
            }
            [self.collectionView insertItemsAtIndexPaths:indexPaths];
            [self.collectionView.mj_footer endRefreshing];
        } else {
            [self initViews];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (void)initViews
{
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    layout.minimumLineSpacing = 8;
    layout.minimumInteritemSpacing = 8;
    layout.sectionInset = UIEdgeInsetsMake(8, 8, 8, 8);
    CGFloat width = floor((UIScreen.mainScreen.bounds.size.width - 24) / 2);
    layout.itemSize = CGSizeMake(width, floor(width * 1.5 + 50));
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    collectionView.backgroundColor = UIColor.lightGrayColor;
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [collectionView registerClass:VideoGridCell.class forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
    
    collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadInfo)];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [GKVideoPlayerManager.manager scrollViewDidEndDecelerating:scrollView];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [GKVideoPlayerManager.manager scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.infos.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    VideoGridCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.info = self.infos[indexPath.item];
    
    return cell;
}

- (UIUserInterfaceStyle)overrideUserInterfaceStyle
{
    return UIUserInterfaceStyleLight;
}

@end
